// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class MainManager extends cc.Component {

    @property(cc.Button)
    btnStart: cc.Button = null;

    @property(cc.ProgressBar)
    loadingBar: cc.ProgressBar = null;

    isFinished: boolean = false;

    onLoad() {

    }

    start() {

    }

    update(dt) { }

    btnStartPlay() {
        this.loadingBar.node.active = true;
        this.btnStart.node.active = false;
        this.loadingBar.progress = 0;
        this.loadingBar.barSprite.fillRange = 0;
        cc.loader.onProgress = (count, amount, item) => {
            if (this.isFinished == false) {
                let progress = Number((count / amount).toFixed(2));
                if (progress > this.loadingBar.barSprite.fillRange) {
                    this.loadingBar.barSprite.fillRange = count / amount;
                }
            }
        };
        cc.director.preloadScene("game_scene", function () {
            if (this.isFinished == false) {
                this.loadingBar.node.active = false;
                this.btnStart.node.active = false;
                console.log("加载成功");
            }

            cc.director.loadScene("game_scene");
            this.isFinished = true;
        }.bind(this));
    }
}
